package cn.net.xyan.easy.spring.core.utils;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by xiashenpin on 16/1/20.
 */
public class ConcurrentTestUtils {
    static public Long concurrentRun(int numberOfThreads, final int perThreadRunTimes, final Runnable runnable) throws InterruptedException {
        List<Thread> threads = new LinkedList<>();
        long begin = System.currentTimeMillis();
        for (int i=0;i<numberOfThreads;i++){
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int t=0;t<perThreadRunTimes;t++){
                        runnable.run();
                    }
                }
            });
            threads.add(thread);
            thread.start();
        }

        for (Thread thread:threads){
            thread.join();
        }

        return System.currentTimeMillis()-begin;
    }
}
